package com.usian.article.controller;

import com.usian.article.ApAuthorControllerApi;
import com.usian.article.service.ApAuthorService;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/apauthor")
public class ApAuthorController implements ApAuthorControllerApi {

    @Autowired
    private ApAuthorService apAuthorService;

    /**
     * 根据用户查询作者是否存在
     *
     * @param userId
     * @return
     */
    @GetMapping("/findByUserId/{userId}")
    @Override
    public ApAuthor findByUserId(@PathVariable Integer userId) {
        return apAuthorService.findByUserId(userId);
    }

    /**
     * 保存作者
     *
     * @param apAuthor
     * @return
     */
    @PostMapping("/save")
    @Override
    public ResponseResult saveAuthor(@RequestBody ApAuthor apAuthor) {
        boolean b = apAuthorService.saveAuthor(apAuthor);
        if(b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    /**
     * 根据作者id查询对象 提供文章审核使用
     *
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findById/{id}")
    public ApAuthor findById(@PathVariable Integer id) {
        return apAuthorService.getById(id);
    }


}
